//
//  WMHorseRaceLampView.m
//  WMHorseRaceLampView
//
//  Created by 徐秀滨 on 2019/12/16.
//  Copyright © 2019 ZoeHealth. All rights reserved.
//

#import "WMHorseRaceLampView.h"

@interface WMHorseRaceLampView()

@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation WMHorseRaceLampView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.fontSize = 14;
    [self config];
    [self updateFrame];
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.fontSize = 14;
    [self config];
    [self updateFrame];
}

- (void)config {
    self.clipsToBounds = YES;
    self.interval = 3;
    
    _label1 = [[UILabel alloc] init];
    _label1.font = [UIFont systemFontOfSize:self.fontSize];
    _label1.textColor = [UIColor colorWithRed:74/255.0 green:74/255.0 blue:74/255.0 alpha:1];
    _label1.numberOfLines = 1;
    _label1.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    _label2 = [[UILabel alloc] init];
    _label2.font = [UIFont systemFontOfSize:self.fontSize];
    _label2.textColor = [UIColor colorWithRed:74/255.0 green:74/255.0 blue:74/255.0 alpha:1];
    _label2.numberOfLines = 1;
    _label2.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    [self addSubview:_label1];
    [self addSubview:_label2];
    
    //添加frame改变通知
    [self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:nil];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
    [self addGestureRecognizer:tap];
}

- (void)setFontSize:(CGFloat)fontSize {
    _fontSize = fontSize;
    self.label1.font = [UIFont systemFontOfSize:self.fontSize];
    self.label2.font = [UIFont systemFontOfSize:self.fontSize];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if([keyPath isEqualToString:@"bounds"]){
        [self updateFrame];
    }
}

- (void)updateFrame {
    _label1.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    _label2.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    self.currentIndex = 0;
    if(_dataSource.count > 0){
        self.label1.text = _dataSource[0];
    }
    if(_dataSource.count > 1){
        self.label2.text = _dataSource[1];
    }
    [self resizeView];
}

- (void)resizeView {
    if(self.dataSource.count == 0){
        return;
    }
    //对数据源遍历找出最大的高度
    CGFloat height = 0;
    for(NSString *content in self.dataSource){
        CGFloat temp = [content boundingRectWithSize:CGSizeMake(self.bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.fontSize]} context:nil].size.height;
        if(temp > height){
            height = temp;
        }
    }
    CGRect frame = self.frame;
    frame.size.height = ceil(height);
    self.frame = frame;
    [self updateFrame];
}

- (void)deal {
    [UIView animateWithDuration:self.interval - 1 animations:^{
        CGRect frame = self.label1.frame;
        frame.origin.y = - self.bounds.size.height;
        self.label1.frame = frame;
        frame = self.label2.frame;
        frame.origin.y = 0;
        self.label2.frame = frame;
    } completion:^(BOOL finished) {
        self.currentIndex ++;
        if(self.currentIndex >= self.dataSource.count){
            self.currentIndex = 0;
        }
        
        self.label1.text = self.dataSource[self.currentIndex];
        CGRect frame = self.label1.frame;
        frame.origin.y = 0;
        self.label1.frame = frame;
        
        frame = self.label2.frame;
        frame.origin.y = self.bounds.size.height;
        self.label2.frame = frame;
        
        NSInteger nextIndex = self.currentIndex + 1;
        if(nextIndex >= self.dataSource.count){
            nextIndex = 0;
        }
        self.label2.text = self.dataSource[nextIndex];
    }];
}

- (void)startRace {
    if(self.dataSource.count < 2){
        return;
    }
    self.currentIndex = 0;
    self.label1.text = _dataSource[0];
    self.label2.text = _dataSource[1];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(deal) userInfo:nil repeats:YES];
}

- (void)endRace {
    if(self.timer){
        [self.timer setFireDate:[NSDate distantFuture]];
        self.timer = nil;
    }
}

- (void)click {
    if([self.delegate respondsToSelector:@selector(clickAtIndex:)]){
        [self.delegate clickAtIndex:self.currentIndex];
    }
}

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"bounds"];
}

@end

